package com.glory.brepda.activity.check;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.QCCheckProgressAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.entity.QCCheckProgressBean;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.apache.commons.lang3.StringUtils;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * QC检验
 */
public class QCCheckProgressActivity extends BaseActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.equipmentId)
    FreeUI_ClearEditText equipmentId;

    @BindView(R.id.sourceChannel)
    FrameLayout sourceChannel;

    @BindView(R.id.sourceChannelLine)
    TextView sourceChannelLine;

    @BindView(R.id.arrow)
    ImageView arrow;


    @BindView(R.id.rvQueryMainTenanceProgressList)
    RecyclerView rvQueryMainTenanceProgressList;

    private FreeUI_GeneralPop parentStatePop;

    private QCCheckProgressAdapter mAdapter;

    private Set<String> sourceMap = new HashSet<>();

    private Map<String,QCCheckProgressBean.QCSoureChannelEntity> sourceChannelMap = new HashMap<>();


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_qccheckprogress;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initView() {

        setMainLayout(llMain);
        tvTitle.setText("QC检验进度查询");
        tvBaseSubmit.setText("查询");
        tvBaseSubmit.setVisibility(View.VISIBLE);
        initRecyclerView(rvQueryMainTenanceProgressList,this);
        mAdapter = new QCCheckProgressAdapter(null,QCCheckProgressActivity.this);
        mAdapter.isFirstOnly(true);
        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        rvQueryMainTenanceProgressList.setAdapter(mAdapter);


    }

    @Override
    protected void initData() {
        queryFormSourceChannel();//qc任务来源渠道

    }

    @OnClick({R.id.sourceChannel,R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.sourceChannel) {

            parentStatePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), sourceChannel.getWidth(), new ArrayList<>(sourceMap), arrow, (codeS, i) -> {
                sourceChannelLine.setText(codeS);
            });
            FreeApi_PopUtils.setArrowShow(arrow);
            parentStatePop.showAsDropDown(sourceChannel, 0, 0);
        }else if (view.getId() == R.id.tvBaseSubmit){
            if(checkIsNull(equipmentId.getText()) ){
                showToast("设备编号必送", false);
                return;
            }
            if(checkIsNull(sourceChannelLine.getText()) ){
                showToast("qc任务来源渠道必送", false);
                return;
            }
            queryCheck();
        }

    }


    public boolean checkIsNull(Object str){
        return str == null || StringUtils.isEmpty(str.toString()) || StringUtils.isBlank(str.toString());
    }

    private void queryCheck(){

        FreeUI_EntityCallBack<BaseResponseBean<QCCheckProgressBean>> callback = new FreeUI_DialogEntityCallBack<BaseResponseBean<QCCheckProgressBean>>
                (new TypeToken<BaseResponseBean<QCCheckProgressBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<QCCheckProgressBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {

                    mAdapter.setNewData(response.body().getResponse().getBody().getQcMaintenanceProgressList());
                    mAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<QCCheckProgressBean>> response) {
                super.onError(response);
                loadError(response.getException(), "QUERYMAINTENANCEPROGRESS");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("EQUIPMENTID", getText(equipmentId));
        if(sourceChannelMap!=null){
            map.addProperty("TASKSOURCE",sourceChannelMap.get(sourceChannelLine.getText().toString()).getKey());
        }

        map.addProperty("ACTIONTYPE","QUERYMAINTENANCEPROGRESS");
        NetUtils.requestPostNet("IF.CPCQCFORMWORKING", "Common", QCCheckProgressActivity.this, map, callback);

    }

    private void queryFormSourceChannel(){

        FreeUI_EntityCallBack<BaseResponseBean<QCCheckProgressBean>> callback = new FreeUI_DialogEntityCallBack<BaseResponseBean<QCCheckProgressBean>>
                (new TypeToken<BaseResponseBean<QCCheckProgressBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<QCCheckProgressBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    List<QCCheckProgressBean.QCSoureChannelEntity> qcSoureChannelList = response.body().getResponse().getBody().getQcSourceChannelList();
                    for(QCCheckProgressBean.QCSoureChannelEntity channelList : qcSoureChannelList ){
                        sourceMap.add(channelList.getText());
                        sourceChannelMap.put(channelList.getText(),channelList);
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<QCCheckProgressBean>> response) {
                super.onError(response);
                loadError(response.getException(), "QUERYQCSOURCECHANNEL");
            }
        };
        sourceMap.clear();
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","QUERYQCSOURCECHANNEL");
        NetUtils.requestPostNet("IF.CPCQCFORMWORKING", "Common", QCCheckProgressActivity.this, map, callback);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1 && resultCode == RESULT_OK) {
            queryCheck();
        }
    }
}